<template>
    <div class="channel-edit">
        <van-cell :border="false">
            <div slot="title" class="title-text">我的频道</div>
            <van-button
            type="danger"
            plain round size="mini"
            class="edit-btn"
            @click="isEdit=!isEdit">
                {{isEdit ? '完成':'编辑'}}
            </van-button>
        </van-cell>

        <van-grid :gutter="10" class="my-grid">
            <van-grid-item
            v-for="(channel,i) in myChannels" :key="i"
            class="grid-item"
            @click="myChannelClick(channel,i)">
                <van-icon slot="icon" name="clear" v-show="isEdit && !fixedChannels.includes(channel.id)"></van-icon>
                <span slot="text" class="text" :class="{active: i===active}">{{channel.name}}</span>
            </van-grid-item>
        </van-grid>

        <van-cell :border="false">
            <div slot="title" class="title-text">频道推荐</div>
        </van-cell>

        <van-grid :gutter="10" class="recommend-grid">
            <van-grid-item
            v-for="(recommendchannel,i) in recommendChannels" :key="i"
            class="grid-item"
            icon="plus"
            :text="recommendchannel.name"
            @click="addChannel(recommendchannel)" />
        </van-grid>
    </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'

export default {
    name: 'ChannelEdit',
    props: ['myChannels', 'active'],
    data () {
        return {
            allChannels: [],
            isEdit: false,
            fixedChannels: [0] // 固定频道，不允许删除
        }
    },
    computed: {
        ...mapState(['user']),
        recommendChannels () {
            // const recommends = []
            // this.allChannels.forEach(channel => {
            //     const res = this.myChannels.find(myChannel => {
            //         return myChannel.id === channel.id
            //     })
            //     if (!res) {
            //         recommends.push(channel)
            //     }
            // })
            // return recommends
            return this.allChannels.filter(channel => {
                return !this.myChannels.some(myChannel => {
                    return myChannel.id === channel.id
                })
            })
        }
    },
    methods: {
        async loadAllChannels () {
            try {
                const res = await getAllChannels()
                this.allChannels = res.data.data.channels
            } catch (err) {
                this.$toast('获取频道列表失败')
            }
        },
        async addChannel (channel) {
            this.myChannels.push(channel)

            // 调用接口保存到后端
            if (this.user) {
                // 已登录，数据更新到线上
                try {
                    await addUserChannel({
                        id: channel.id,
                        seq: this.myChannels.length // 序号
                    })
                } catch (err) {
                    this.$toast('保存失败，请稍后重试')
                }
            } else {
                // 未登录，数据更新到本地存储
                setItem('TOUTIAO_CHANNELS', this.myChannels)
            }
        },
        myChannelClick (channel, i) {
            if (this.isEdit) {
                // 固定频道不允许删除
                if (this.fixedChannels.includes(channel.id)) {
                    return
                }

                // 如果删除的是高亮之前的，让其索引-1
                if (i <= this.active) {
                    this.$emit('changeActive', this.active - 1)
                }

                this.myChannels.splice(i, 1)
                this.deleteChannel(channel)
            } else {
                this.$emit('changeActive', i, false)
            }
        },
        async deleteChannel (channel) {
            try {
                if (this.user) {
                    await deleteUserChannel(channel.id)
                } else {
                    setItem('TOUTIAO_CHANNELS', this.myChannels)
                }
            } catch (err) {
                this.$toast('删除失败，请稍后重试')
            }
        }
    },
    created () {
        this.loadAllChannels()
    }
}
</script>

<style scoped lang="less">
.channel-edit {
    padding: 85px 0;
    .title-text {
        font-size: 32px;
        color: #333333;
    }
    .edit-btn {
        width: 104px;
        height: 48px;
        font-size: 26px;
        color: #f85959;
        border: 1px solid #f85959;
    }
    /deep/ .grid-item {
        width: 160px;
        height: 86px;
        .van-grid-item__content {
            background-color: #f1f1f1;
            white-space: nowrap;
            .van-grid-item__text, .text {
                font-size: 28px;
                color: #222;
                margin-top: 0;
            }
            .active {
                color: red;
            }
            .van-grid-item__icon-wrapper {
                position: unset;
            }
        }
    }
    /deep/ .my-grid {
        .grid-item {
            .van-icon-clear {
                position: absolute;
                right: -10px;
                top: -10px;
                font-size: 30px;
                color: #cacaca;
                z-index: 2;
            }
        }
    }
    /deep/ .recommend-grid {
        .grid-item {
            .van-grid-item__content {
                flex-direction: row;
                .van-icon-plus {
                    font-size: 28px;
                    margin-right: 6px;
                }
                .van-grid-item__text {
                    margin-top: 0;
                }
            }
        }
    }
}
</style>
